package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Tuition;
import com.example.result.Result;
import com.example.service.TuitionService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 学费控制层
 * @date 2020-01-01
 */
@RestController
@RequestMapping("/api")
public class TuitionController {

    @Autowired
    TuitionService tuitionService;

    //==================================学费管理===============================================

    /**
     * @param json
     * @return 添加成功则返回成功提示信息，反之则提示添加失败信息
     * @describe 添加学费信息
     */
    @RequestMapping(value = "/tuitions", method = RequestMethod.POST)
    public Result addTuition(@RequestBody String json) {

        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Tuition tuition = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Tuition.class);

        return tuitionService.addTuition(tuition);
    }

    /**
     * @param json
     * @return 更新成功则返回成功提示信息，反之则提示更新失败信息
     * @describe 更新学费信息
     */
    @RequestMapping(value = "/tuitions", method = RequestMethod.PUT)
    public Result updateTuition(@RequestBody String json) {

        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Tuition tuition = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Tuition.class);

        return tuitionService.updateTuition(tuition);
    }

    /**
     * @param tuition_Id
     * @return 删除成功则返回成功提示信息，反之则提示删除失败信息
     * @describe 根据编号删除学费信息
     */
    @RequestMapping(value = "/tuitions/{tuition_Id}", method = RequestMethod.DELETE)
    public Result deleteTuitionById(@PathVariable("tuition_Id") String tuitionId) {

        return tuitionService.deleteTuitionById(tuitionId);
    }

    /**
     * @return 以JSON格式返回所有学费信息
     * @describe 查询所有学费信息
     */
    @RequestMapping(value = "/tuitions", method = RequestMethod.GET)
    public Result queryAllTuition() {

        return tuitionService.queryAllTuition();
    }

    /**
     * @param tuition_Id
     * @return 以JSON格式返回该学费信息
     * @describe 根据编号查询该学费信息
     */
    @RequestMapping(value = "/tuitions/{tuition_Id}", method = RequestMethod.GET)
    public Result queryTuitionById(@PathVariable("tuition_Id") String tuitionId) {

        return tuitionService.queryTuitionById(tuitionId);
    }

    /**
     * @return 以JSON格式返回该学费和学期信息
     * @describe 查询所有学期和学费信息
     */
    @RequestMapping(value = "/tuitions/all", method = RequestMethod.GET)
    public Result queryTermAndTuitionAll() {

        return tuitionService.queryTermAndTuitionAll();
    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加学费信息
     */
    @RequestMapping(value = "/tuitions/excels", method = RequestMethod.POST)
    public Result addTuitionByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<Tuition> list = JSON.parseArray(parseObject.getString("json"), Tuition.class);

        System.out.println(list);

        return tuitionService.addTuitionByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学费编号批量删除学期信息
     */
    @RequestMapping(value = "/tuitions/dels", method = RequestMethod.POST)
    public Result deleteTuitionByIds(@RequestBody Map<String, String> Ids) {

        return tuitionService.deleteTuitionByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 学费信息模糊查询
     */
    @RequestMapping(value = "/tuitions/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return tuitionService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询没有交费的班级学生
     */
    @RequestMapping(value = "/tuitions/studentTuitionById", method = RequestMethod.GET)
    public Result queryStudentIdNotInTuition() {
        return tuitionService.queryStudentIdNotInTuition();

    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生ID查询学费信息
     */
    @RequestMapping(value = "/tuitions/tuitionByUserId", method = RequestMethod.POST)
    public Result selectTuitionByUserId(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        Integer userId = Integer.parseInt(parseObject.getString("json"));

        return tuitionService.selectTuitionByUserId(userId);
    }

}
